package com.larissa.android.quiz;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.larissa.android.quiz.databinding.FragmentHistoryBinding;
import com.larissa.android.quiz.databinding.FragmentMainBinding;

import java.util.List;

public class HistoryFragment extends Fragment {

    private MainViewModel mainViewModel;

    private List<Question> questions;
    private FragmentHistoryBinding fragmentHistoryBinding;
    private int flag;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainViewModel = new ViewModelProvider(requireActivity()).get(MainViewModel.class);
        flag = mainViewModel.statusFlag;;
        questions = mainViewModel.historyQuestions;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        fragmentHistoryBinding = FragmentHistoryBinding.inflate(inflater, container, false);
        return fragmentHistoryBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView=view.findViewById(R.id.history_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(new HistoryItemAdapter(questions.size()));
    }

    private class HistoryItemViewHolder extends RecyclerView.ViewHolder
    {
        private TextView historyText;
        private TextView historyAnswer;

        private int index;

        public  HistoryItemViewHolder(@NonNull View itemView)
        {
            super(itemView);
            historyText=itemView.findViewById(R.id.history_text);
            historyAnswer=itemView.findViewById(R.id.history_answer);
        }

        private void bind(int index)
        {
            this.index = index;
            historyText.setText(questions.get(index).getQuestionText());
            historyAnswer.setText(questions.get(index).getQuestionAnswer());
        }
    }

    private class HistoryItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
    {
        private int count;
        public HistoryItemAdapter(int count){this.count = count;}

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=getLayoutInflater().inflate(R.layout.history_item,parent,false);
            return new HistoryItemViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((HistoryItemViewHolder)holder).bind(position);
        }

        @Override
        public int getItemCount() {
            return count;
        }
    }

}
